(function($){
	//自定义CK上传图片插件
	$.addCKImage = function(myPlugun, uploadUrl){
		//var myPlugun = "myImage";
		CKEDITOR.plugins.add(myPlugun , {
			init : function(editor){
				editor.addCommand(myPlugun, {exec : function(editor){
					var $html = $("#dd").clone().show();
					var $url = $html.find("input[name=pic]");
					
					var $box = $.box({
						width:500,
						title:"添加图片",
						message:$html,
						ok:function(){
							if($url.val().length <= 0){
								return;
							}
							var src = $url.val();
							src && editor.insertHtml("\n<div style='text-align:center;'><img src='"+src+"' /></div>\n");
							$url.val("");
							$html.find("img").attr("src",$url.val());
							$html.find(":file").attr("src",$url.val());
						},
						cancel:function(){
							$url.val("");
							$html.find("img").attr("src",$url.val());
							$html.find(":file").attr("src",$url.val());
						}
					});
					$html.delegate(":file","change",function(){
						var $file = $(this);
						if(this.value.length <= 0){
							return;
						}
						
						//下面的上传路径要重新定义
						$(this).uploadFile(uploadUrl, function(data){
							if(data.hasError){
								alert(data.message);
								return;
							}
							$url.val(data.url);
							$html.find("#imgDiv").html("<img src='"+data.url+"' />");
						});
						$file.val("");
					});
					$box.hide().css("z-index", "99999").show();
				}});
				
				editor.ui.addButton(myPlugun , {
					label : "添加图片",
					//icon  : $.frontPath + "/img/myImage.png",
					command : myPlugun
					}
				);
			}
		});
	};
	
	$.addCKVideo = function(myPlugun){
		//var myPlugun = "myImage";
		CKEDITOR.plugins.add(myPlugun , {
			init : function(editor){
				editor.addCommand(myPlugun, {exec : function(editor){
					var $html = $("#ddVideo").clone().show();
					var $url = $html.find("input[name=vid]");
					
					var $box = $.box({
						width:500,
						title:"添加视频",
						message:$html,
						ok:function(){
							if($url.val().length <= 0){
								return;
							}
							var src = $url.val().trim();
							if(src.match(".swf$")){
								src = encodeURIComponent(src);
								editor.insertHtml("<img class='cke_flash' data-cke-realelement='%3Ccke%3Aobject%20align%3D%22undefined%22%20classid%3D%22clsid%3Ad27cdb6e-ae6d-11cf-96b8-444553540000%22%20codebase%3D%22http%3A%2F%2Fdownload.macromedia.com%2Fpub%2Fshockwave%2Fcabs%2Fflash%2Fswflash.cab%23version%3D6%2C0%2C40%2C0%22%20height%3D%22333%22%20hspace%3D%225%22%20id%3D%22undefined%22%20vspace%3D%2210%22%20width%3D%22555%22%20style%3D%22undefined%22%3E%3Ccke%3Aparam%20name%3D%22quality%22%20value%3D%22high%22%3E%3C%2Fcke%3Aparam%3E%3Ccke%3Aparam%20name%3D%22movie%22%20value%3D%22"+ src +"%22%3E%3C%2Fcke%3Aparam%3E%3Ccke%3Aembed%20height%3D%22333%22%20pluginspage%3D%22http%3A%2F%2Fwww.macromedia.com%2Fgo%2Fgetflashplayer%22%20quality%3D%22high%22%20src%3D%22"+ src +"%22%20type%3D%22application%2Fx-shockwave-flash%22%20width%3D%22555%22%3E%3C%2Fcke%3Aembed%3E%3C%2Fcke%3Aobject%3E' data-cke-real-node-type='1' src='' data-cke-real-element-type='flash' data-cke-resizable='true' style='width:555px;height:333px;' />");
							}else{
								alert("添加的不是flash");
								return false;
							}
							$url.val("");
						},
						cancel:function(){
							$url.val("");
							$html.find("img").attr("src",$url.val());
							$html.find(":file").attr("src",$url.val());
						}
					});
					$box.hide().css("z-index", "99999").show();
				}});
				
				editor.ui.addButton(myPlugun , {
					label : "添加视频",
					//icon  : $.frontPath + "/img/myImage.png",
					command : myPlugun
				});
			}
		});
	};
	
	//表情插件
	var Emoji = function(opt){
		opt = opt || {};
		
		var _this = this;
		_this.emojiPath  = window.CM.emojiPath || "";
		_this.emoji = window.CM.emoji || [];
		
		_this.emojiClick = opt.emojiClick || function(){
			return this;
		};
		
		var $html = $('<div class="emojis-box"><div class="emojis-list"><span class="right"><a href="javascript:;" class="current">&lt;</a><a href="javascript:;">&gt;</a></span><ul class="emojis-list-tab"></ul></div><ul class="emojis fn-clear"></ul></div>');
		var $tab = $html.find(".emojis-list-tab");
		var $lt = $html.find(".right a:eq(0)");
		var $gt = $html.find(".right a:eq(1)");
		var $list = $html.find(".emojis");
		
		var index = 0;
		var maxIndex = Math.floor(_this.emoji.length / 5)-1;
		var _category = "默认";
		
		//初始化
		this.init = function(){
			index = (index*1 || 0) < 0 ? 0 : index;
			index = index > maxIndex ? maxIndex : index;
			if(index == 0){
				$lt.addClass("current");
				$gt.removeClass("current");
			}else if(index == maxIndex){
				$gt.addClass("current");
				$lt.removeClass("current");
			}
			var begin = index*5 , end = (index+1)*5;
			
			//分组导航
			$tab.empty();
			for(; begin<end; begin++){
				var _e = _this.emoji[begin];
				var _$li = $("<li><a href='javascript:;' data-category='" + _e.category + "'>" + _e.category + "</a></li>");
				_category ==  _e.category && _$li.find("a").addClass("current");
				$tab.append(_$li);
			}
			
			//分组点击
			var $a = $tab.find("[data-category]");
			$a.click(function(){
				$a.removeClass("current");
				var $this = $(this).addClass("current");
				_category = $this.data("category");
				var _emoji = {};
				$.each(_this.emoji, function(i, _e){
					if(_e.category == _category){
						_emoji = _e.data || {};
						return false;
					}
				});
				
				$list.empty();
				for(var i=0; i<_emoji.length; i++){
					var _e = _emoji[i];
					var $li = $("<li><img></li>");
					$li.find("img").attr({title:_e.phrase,alt:_e.phrase,src:_this.emojiPath+_e.url}).click(_this.emojiClick);
					$list.append($li);
				}
			});
			!!_category && $a.filter("[data-category="+ _category + "]").click();
		};
		
		$lt.click(function(){
			--index;
			_this.init();
		});
		$gt.click(function(){
			++index;
			_this.init();
		});
		
		//显示
		this.show = function(){
			_this.init();
			var $box = $.box({
				title:"表情",
				width:475,
				html:$html
			});
			$box.find(".body").css({"padding":0});
			$("#emoji-left").show();
			$box.find(".bottom .right").hide();
		};
		_this.show();
	};
	
	//自定义CK表情
	$.addCKEmoji = function(myPlugun){
		CKEDITOR.plugins.add(myPlugun , {
			init : function(editor){
				editor.addCommand(myPlugun, {exec : function(editor){
					Emoji({
						emojiClick:function(e){
							editor.insertHtml(this.outerHTML);
							!e.ctrlKey && $.box("close");
						}
					});
				}});
				
				editor.ui.addButton(myPlugun , {
					label : "添加表情",
					command : myPlugun
				});
			}
		});
	};
	
	//获取表情的简单代码
	CKEDITOR.tools.extend(CKEDITOR.editor.prototype, {getSimpleData: function() {
		var d = this.getData();
		if( !d ) return d;
		
		d = d.replace(/\r|\n/ig,"");
		
		var $d = $("<div>" + d + "</div>");
		$d.find("img[alt*='[']").each(function(){
			var $img = $(this);
			$img.replaceWith( $img.prop("alt") );
		});
		return $d.html();
	}});
	
})(window.jQuery);



